package com.lenovo.smarttraffic.ui.activity;

import android.annotation.SuppressLint;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.CircleOptions;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.GongJiaoBean_7;
import com.lenovo.smarttraffic.bean.XianLuBean_6;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Activity_6 extends AppCompatActivity {
    private MapView a_6_mapview;
    private ImageView a_6_imgleft;
    private ImageView a_6_img1;
    private ImageView a_6_img2;
    private ImageView a_6_img3;
    private ImageView a_6_img4;
    private TextView a_6_tv1;
    AMap aMap;
    RequestQueue queue;
    List<XianLuBean_6.RouteBean> beans = new ArrayList<>();

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_6);
        initView();

        a_6_mapview.onCreate(savedInstanceState);

        if(aMap == null){
            aMap = a_6_mapview.getMap();
        }

        a_6_imgleft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        LatLng latLng1 = new LatLng(39.941853,116.385307);
        LatLng latLng2 = new LatLng(39.941003,116.105307);
        LatLng latLng3 = new LatLng(39.014453,116.300007);
        LatLng latLng4 = new LatLng(39.903523,116.305207);

        List<LatLng> latLngs = new ArrayList<>();
        latLngs.add(latLng1);
        latLngs.add(latLng2);
        latLngs.add(latLng3);
        latLngs.add(latLng4);

        a_6_img3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                aMap.addMarker(new MarkerOptions().position(latLng1).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.marker_one))).title("一号小车\n联想大厦园区"));
                aMap.addMarker(new MarkerOptions().position(latLng2).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.marker_second))).title("二号小车\n联大想厦园区"));
                aMap.addMarker(new MarkerOptions().position(latLng3).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.marker_thrid))).title("三号小车\n联想厦大园区"));
                aMap.addMarker(new MarkerOptions().position(latLng4).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.marker_forth))).title("四号小车\n想大联厦园区"));
                a_6_tv1.setText("1，2，3，4号小车已完成标记");
            }
        });

        //连接4个小车位子线条
        aMap.addPolyline(new PolylineOptions().color(Color.RED).addAll(latLngs));
        aMap.addCircle(new CircleOptions().strokeWidth(15).radius(1000).center(latLng2).strokeColor(Color.BLACK));

        //点击定位到2号小车位置
        a_6_img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng2,12,0,0));
                aMap.moveCamera(cameraUpdate);
            }
        });

        getdata();

    }

    private void getdata() {

        JSONObject object = new JSONObject();
        try {
//            "UserName":"user1","origin":"首都机场","destination":"北京西站"
            object.put("UserName", "user1");
            object.put("origin", "首都机场");
            object.put("destination", "北京西站");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request = new JsonObjectRequest(JsonRequest.Method.POST, "http://192.168.1.107:8088/transportservice/action/GetBusInfo.do", object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
//                beans = new Gson().fromJson(jsonObject.toString(), XianLuBean_6.class).getRoute();

                Log.i("getOrigin", beans.get(0).getOrigin());
                Log.i("getDestination", beans.get(0).getDestination());
                Log.i("getDistance", "距离"+beans.get(0).getPaths().get(0).getDistance());


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        queue.add(request);

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        a_6_mapview.onSaveInstanceState(outState);
    }

    private void initView() {

        a_6_mapview = (MapView) findViewById(R.id.a_6_mapview);
        a_6_imgleft = (ImageView) findViewById(R.id.a_6_imgleft);
        a_6_img1 = (ImageView) findViewById(R.id.a_6_img1);
        a_6_img2 = (ImageView) findViewById(R.id.a_6_img2);
        a_6_img3 = (ImageView) findViewById(R.id.a_6_img3);
        a_6_img4 = (ImageView) findViewById(R.id.a_6_img4);
        a_6_tv1 = (TextView) findViewById(R.id.a_6_tv1);

        queue = Volley.newRequestQueue(Activity_6.this);

    }
}